package com.rabbitmq.rabbitmqextensions.config;

import com.rabbitmq.rabbitmqextensions.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;

@Configuration
public class RabbitMQConfig {

    @Bean("ackQueue")
    public Queue ackQueue() {
        return QueueBuilder.durable(Constants.ACK_QUEUE).build();
    }

    @Bean("ackExchange")
    public DirectExchange ackExchange() {
        return ExchangeBuilder.directExchange(Constants.ACK_EXCHANGE).build();
    }

    @Bean("ackBind")
    public Binding ackBind(@Qualifier("ackExchange") DirectExchange directExchange,
                           @Qualifier("ackQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.ACK_ROUTINGKEY);
    }

    /**
     * 持久化队列
     */
    @Bean("persQueue")
    public Queue persQueue() {
        return QueueBuilder.durable(Constants.PERS_QUEUE).build();
    }

    /**
     * 非持久化队列
     */
    @Bean("notPersQueue")
    public Queue notPersQueue() {
        return QueueBuilder.nonDurable(Constants.NOT_PERS_QUEUE).build();
    }

    /**
     * 持久化交换机
     */
    @Bean("persExchange")
    public DirectExchange persExchange() {
//        return ExchangeBuilder.directExchange(Constants.PRES_EXCHANGE).build();
        return ExchangeBuilder.directExchange(Constants.PRES_EXCHANGE).durable(true).build();
    }

    /**
     * 非持久化交换机
     */
    @Bean("notPersExchange")
    public DirectExchange notPersExchange() {
        return ExchangeBuilder.directExchange(Constants.NOT_PERS_EXCHANGE).durable(false).build();
    }

    /**
     * 绑定交换机与队列
     */
    @Bean("bind1")
    public Binding bind1(@Qualifier("persExchange") DirectExchange directExchange,
                         @Qualifier("persQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.PERS_ROUTINGKEY);
    }
    @Bean("bind2")
    public Binding bind2(@Qualifier("notPersExchange") DirectExchange directExchange,
                         @Qualifier("notPersQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.NPERS_ROUTINGKEY);
    }

    /**
     * 确认模式
     */
    @Bean("confirmQueue")
    public Queue confirmQueue() {
        return QueueBuilder.durable(Constants.CONFIRM_QUEUE).build();
    }
    @Bean("confirmExchange")
    public DirectExchange confirmExchange() {
        return ExchangeBuilder.directExchange(Constants.CONFIRM_EXCHANGE).build();
    }
    @Bean("confirmBind")
    public Binding confirmBind(@Qualifier("confirmExchange") DirectExchange directExchange,
                               @Qualifier("confirmQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.CONFIRM_ROUTINGKEY);
    }

    /**
     * 重试机制
     */
    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }
    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).build();
    }
    @Bean("retryBind")
    public Binding retryBind(@Qualifier("retryExchange") DirectExchange directExchange,
                             @Qualifier("retryQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.RETRY_ROUTINGKEY);
    }

    /**
     * ttl
     * @return
     */
    @Bean("ttlQueue1")
    public Queue ttlQueue1() {
        return QueueBuilder.durable(Constants.TTL_QUEUE1).build();
    }
    @Bean("ttlQueue2")
    public Queue ttlQueue2() {
        return QueueBuilder.durable(Constants.TTL_QUEUE2).ttl(15000).build(); //设置过期时间为 15s
    }
    @Bean("ttlExchange")
    public DirectExchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).build();
    }
    @Bean("ttlBind1")
    public Binding ttlBind1(@Qualifier("ttlExchange") DirectExchange directExchange,
                           @Qualifier("ttlQueue1") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.TTL_ROUTINGKEY);
    }
    @Bean("ttlBind2")
    public Binding ttlBind2(@Qualifier("ttlExchange") DirectExchange directExchange,
                           @Qualifier("ttlQueue2") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.TTL_ROUTINGKEY);
    }

    /**
     * 事务
     */
    @Bean("transQueue")
    public Queue transQueue() {
        return QueueBuilder.durable(Constants.TRANS_QUEUE).build();
    }

    /**
     * 限流
     */
    @Bean("qosQueue")
    public Queue qosQueue() {
        return QueueBuilder.durable(Constants.QOS_QUEUE).build();
    }

    @Bean("qoeExchange")
    public DirectExchange qoeExchange() {
        return ExchangeBuilder.directExchange(Constants.QOS_EXCHANGE).build();
    }

    @Bean("qosBind")
    public Binding qosBind(@Qualifier("qoeExchange") DirectExchange directExchange,
                           @Qualifier("qosQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with(Constants.QOS_ROUTINGKEY);
    }
}
